﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LibrarySync2.Business.FileSystem.Item;
using LibrarySync2.Business.FileSystem.Update;
using LibrarySync2.Common.Interfaces;

namespace LibrarySync2.Business.FileSystem
{
    public class FileSystemUpdate : List<IFileSystemItemUpdate>, IFileSystemUpdate
    {
        public string Name { get; private set; }
        public string FullName { get; private set; }
        public bool IncludeMetadata { get; private set; }

        public FileSystemUpdate(string name, string fullName, bool includeMetadata)
        {
            Name = name;
            FullName = fullName;
            IncludeMetadata = includeMetadata;
        }

        internal void SortList()
        {
            Sort(CompareUpdates);
        }

        public int CompareUpdates(IFileSystemItemUpdate in1, IFileSystemItemUpdate in2)
        {
            if (in1 == null)
                throw new ArgumentNullException("in1");
            if (in2 == null)
                throw new ArgumentNullException("in2");

            var fsi1 = in1.MediaFile;
            var fsi2 = in2.MediaFile;
            var fsi1IsDirectory = fsi1 is Directory;
            var fsi2IsDirectory = fsi2 is Directory;

            //2 simple rules:
            //1. Directories go before other types.
            if (fsi1IsDirectory && !(fsi2IsDirectory))
            {
                return -1;
            }

            if (fsi2IsDirectory && !(fsi1IsDirectory))
            {
                return 1;
            }

            //2. Otherwise, shortest file first.
            return (fsi1.RelativePath.CompareTo(fsi2.RelativePath));
        }
    }
}
